#include "Timestamp.h"
#include <sys/time.h>

Timestamp::Timestamp() : microSecondsSinceEpoch_(0)
{
}

Timestamp::Timestamp(int64_t microSecondsSinceEpoch)
    : microSecondsSinceEpoch_(microSecondsSinceEpoch)
{
}

Timestamp Timestamp::now()
{
    struct timeval tv;
    gettimeofday(&tv, nullptr);
    return Timestamp(tv.tv_sec * kMicroSecondsSinceEpoch + tv.tv_usec);
}
std::string Timestamp::toString() const
{
    char buf[128] = {0};
    tm *tmTime = localtime(&microSecondsSinceEpoch_);
    snprintf(buf, 128, "%4d-%02d-%02d %02d:%02d:%02d", tmTime->tm_year + 1900,
             tmTime->tm_mon + 1, tmTime->tm_mday, tmTime->tm_hour,
             tmTime->tm_min, tmTime->tm_sec);
    return buf;
}
